/**
 * 
 */
package com.usc.entme;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * @author pranav
 *
 */
public class EntmeGridViewActivity extends Activity {
	private Context Context;
	private int REQUEST_CODE = 1234;
	private String TAG = "Entme";

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		Context = this;
		setContentView(R.layout.entme);
		GridView g = (GridView) findViewById(R.id.myGrid);
		g.setAdapter(new ImageAdapter(this));
		g.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {				
				Intent intent;
				switch(position){
				case 0:
					intent = new Intent(Context,News3DActivity.class);
					startActivity(intent);
					break;
				case 1:
					intent = new Intent(Context,Movie3DActivity.class);
					startActivity(intent);
					break;
				case 2:
					intent = new Intent(Context,Sports3DActivity.class);
					startActivity(intent);
					break;
				case 3:
					intent = new Intent(Context,Preferences.class);
					startActivity(intent);
					break;
				case 4:
					startVoiceRecognitionActivity();
					break;
				case 5:
					intent = new Intent(Context,HelpActivity.class);
					startActivity(intent);
					break;

				}
			}
		});
	}

	private void startVoiceRecognitionActivity()
	{
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
		startActivityForResult(intent, REQUEST_CODE);
	}

	/**
	 * Handle the results from the voice recognition activity.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (requestCode == REQUEST_CODE  && resultCode == RESULT_OK)
		{
			// Populate the wordsList with the String values the recognition engine thought it heard
			ArrayList<String> matches = data.getStringArrayListExtra(
					RecognizerIntent.EXTRA_RESULTS);
			//      wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
			//            matches));

			if(matches.isEmpty())
			{
				Toast.makeText(Context, "No Match found", 2);
			}
			else
			{
				Boolean flag = false;
				for(int i=0;i<matches.size();i++)
				{
					if(matches.get(i).equalsIgnoreCase("news"))
					{
						Log.d(TAG, "News detected int Vocie Recognition");
						Intent intent = new Intent(Context,News3DActivity.class);
						startActivity(intent);
						flag =true;
						break;
					}
					if(matches.get(i).equalsIgnoreCase("movies"))
					{
						Intent intent = new Intent(Context,Movie3DActivity.class);
						startActivity(intent);
						flag =true;
						break;
					}
					if(matches.get(i).equalsIgnoreCase("sports"))
					{
						Intent intent = new Intent(Context,Sports3DActivity.class);
						startActivity(intent);
						flag =true;
						break;
					}


				}
				if(!flag)
				{

					Toast.makeText(Context, "No Match found. Try Again!!", Toast.LENGTH_LONG);
					Log.d(TAG , "No match found");
				}

			}

		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	public class ImageAdapter extends BaseAdapter {
		public ImageAdapter(Context c) {
			mContext = c;
		}
		public int getCount() {
			return mThumbIds.length;
		}
		public Object getItem(int position) {
			return position;
		}
		public long getItemId(int position) {
			return position;
		}
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView;
			if (convertView == null) {
				imageView = new ImageView(mContext);
				imageView.setLayoutParams(new GridView.LayoutParams(175, 175));
				imageView.setAdjustViewBounds(false);
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				imageView.setPadding(15, 15, 15, 15);
			} else {
				imageView = (ImageView) convertView;
			}
			imageView.setImageResource(mThumbIds[position]);
			return imageView;
		}
		private Context mContext;
		private Integer[] mThumbIds = {
				R.drawable.news, R.drawable.movies,
				R.drawable.sports, R.drawable.settings,
				R.drawable.speech, R.drawable.help
		};
	}
}
